package com.ztesoft.huangfu.learning.springboot.springbootssjwt.web.controller;

import com.ztesoft.huangfu.learning.springboot.springbootssjwt.entity.User;
import com.ztesoft.huangfu.learning.springboot.springbootssjwt.exception.UsernameIsExitedException;
import com.ztesoft.huangfu.learning.springboot.springbootssjwt.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/6/10
 * Time: 10:36
 * Version: 1.0
 * Description:
 **/
@RestController
@RequestMapping("/users")
@Api(value = "注册管理", description = "注册管理")
public class RegisterController extends BaseController {

    /**
     * 注册用户 默认开启白名单
     *
     * @param user
     */
    @ApiOperation(value = "注册用户")
    @PostMapping("/signup")
    public User signup(@RequestBody User user) {
        User bizUser = userdao.findByUsername(user.getUsername());
        if (null != bizUser) {
            throw new UsernameIsExitedException("用户已经存在");
        }
        user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword()).getBytes()));
        user.setId(UUIDUtils.uuid());
        userdao.save(user);
        return user;
    }

}
